package com.dsjy.yf.address.controller;

import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.SplitResult;
import com.dsjy.yf.address.entity.status.AddResult;
import com.dsjy.yf.address.service.AddressSplitService;
import com.dsjy.yf.address.service.BatchProcessService;
import com.dsjy.yf.address.utils.DownLoadUtil;
import com.dsjy.yf.address.utils.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-12 16:12
 */
@Controller
@RequestMapping("/split/")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
@CrossOrigin(origins = "*", originPatterns = "true")
public class AddressController {
    private static final String CSV = ".csv";
    @Autowired
    private AddressSplitService addressSplitService;
    @Autowired
    private BatchProcessService batchProcessService;

    /**
     * 下载模板
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/getTemplate")
    public void getTemplate(HttpServletResponse response) throws IOException {
        DownLoadUtil.downLoadFile(response);
    }

    /**
     * 分割单个地址
     *
     * @param address
     * @return
     */
    @ResponseBody
    @GetMapping("/splitAddress")
    public SplitResult splitAddress(String address) {
        String value = MyStringUtil.removeEmpty(address);
        if (value != null) {
            return addressSplitService.splitAddress(value);
        }
        return null;
    }

    @PostMapping("/batch")
    public void batchProcess(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (file == null || file.isEmpty()) {
            throw new BizException(CommonEnum.FILE_NULL);
        }
        if (file.getSize() > 200 * 1024 * 1024) {
            request.getRequestDispatcher("/address/split/result?message=文件超过限制&status=1")
                    .forward(request, response);
            return;
        }
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            throw new BizException(CommonEnum.FILENAME_NULL);
        } else if (fileName.endsWith(CSV)) {
            batchProcessService.processWithCsv(file, fileName, response);
        } else {
            InputStream inputStream = file.getInputStream();
            batchProcessService.processWithExcel(inputStream, fileName, response);
        }
    }

    @RequestMapping("/result")
    public AddResult giveResult(String message, Integer status) {
        if (status != 0) {
            throw new BizException(500, message);
        }
        return null;
    }

}
